#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPixmap>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <QFont>
#include <QString>
#include <QRect>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>


PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
{

}
PlayScene::PlayScene(int index){
    //设置window属性
    this->levelIndex = index;
    //初始化关卡二维数组(其实就是关卡数据，根据关卡的不同数据自然也就不同)
    dataConfig config;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            gameArray[i][j] = config.mData[this->levelIndex][i][j];
//            config.mData[this->levelIndex][i][j];
        }
    }


    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置窗口图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //标题
    this->setWindowTitle("翻金币");

    //创建菜单栏
    QMenuBar *menuBar = this->menuBar();
    this->setMenuBar(menuBar);
    //开始创建菜单
    QMenu *startMenu = menuBar->addMenu("开始");
    //创建按钮菜单项
    QAction *action = startMenu->addAction("退出");
    //点击后退出游戏
    connect(action,&QAction::triggered,[=](){
        this->close();
    });



    //制作返回按钮
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    //将返回按钮放到对象树
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //返回按钮功能实现
    connect(backBtn,&MyPushButton::clicked,[=](){
        QTimer::singleShot(500,this,[=](){
            this->hide();
            //触发自定义信号，关闭自身。需要把该信号写在signals下做声明
            emit this->chooseSenceBack();
        });
    });



    //设置当前关卡标题
    QLabel *label = new QLabel;
    label->setParent(this);//将QLabel添加到对象树
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level: %1").arg(this->levelIndex);
    label->setText(str);
    label->setGeometry(QRect(30, this->height() - 50,120, 50));//设置大小和位置

    //创建胜利后的图片
    QLabel *winLabel = new QLabel;
    QPixmap winPixmap;
    winPixmap.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,winPixmap.width(),winPixmap.height());
    winLabel->setPixmap(winPixmap);
    winLabel->setParent(this);
    winLabel->move( (this->width() - winPixmap.width())*0.5 , -winPixmap.height());


    //创建金币背景图片
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            //绘制背景
            QLabel *label = new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            label->setParent(this);
            label->move(57 + i*50,200+j*50);


             QString img;
             if(gameArray[i][j] ==1){
                 img =  ":/res/Coin0001.png";
             }else{
                 img = ":/res/Coin0008.png";
             }
            //设置对象
            MyCoin *coin = new MyCoin(img);
            coin->setParent(this);
            coin->move(59 + i*50,204+j*50);
            coin->posX = i; //记录x坐标
            coin->posY = j; //记录y坐标
            coin->flag = gameArray[i][j]; //记录正反标志
            coinBtn[i][j] = coin;//记录每一个按钮的位置




            //监听按钮点击
            connect(coin,&MyCoin::clicked,[=](){
                flipSound->play();//翻金币音效
                coin->changeFlag();
                gameArray[i][j] = gameArray[i][j]==0?1:0;//数组内部记录标志同步修改
                QTimer::singleShot(300, this,[=](){
                    if(coin->posX+1 <=3)
                    {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posX-1>=0)
                    {
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posY+1<=3)
                    {
                        coinBtn[coin->posX][coin->posY+1]->changeFlag();
                        gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    if(coin->posY-1>=0)
                    {
                        coinBtn[coin->posX][coin->posY-1]->changeFlag();
                        gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX+1][coin->posY]== 0 ? 1 : 0;
                    }
                    //判断是否胜利
                    this->isWin = true;
                    for(int i = 0 ; i < 4;i++)
                    {
                        for(int j = 0 ; j < 4; j++)
                        {
    //                        qDebug() << coinBtn[i][j]->flag ;
                            if( coinBtn[i][j]->flag == false)
                            {
                                this->isWin = false;
                                break;
                            }
                        }
                    }


                    //如果isWin依然是true，代表胜利了！
                    if(this->isWin)
                    {
                        qDebug() << "胜利";
                        winSound->play();//胜利音效
                        QPropertyAnimation * animation1 =  new QPropertyAnimation(winLabel,"geometry");
                        animation1->setDuration(1000);
                        animation1->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        animation1->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        animation1->setEasingCurve(QEasingCurve::OutBounce);
                        animation1->start();
                    }
                });


            });



        }
    }


}

void PlayScene::paintEvent(QPaintEvent *event){
    //绘制背景
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pixmap);

    //加载标题
    pixmap.load(":/res/Title.png");
    pixmap = pixmap.scaled(pixmap.width()*0.5,pixmap.height()*0.5);
    painter.drawPixmap(10,30,pixmap.width(),pixmap.height(),pixmap);





}
